from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

from sqlalchemy import Column,Integer,String,ForeignKey  # 字段
from sqlalchemy.orm import relationship  # 表关系

class Student(Base):
    __tablename__ = 'student'
    id = Column(Integer, primary_key=True)
    name = Column(String(32))
    school_id = Column(Integer,ForeignKey("school.id"))  # 数据表的id字段

    # Student.stu2sch 转到 School表中
    stu2sch = relationship("School",backref="sch2stu")  # 创建外键关系  反向关系  学生对学校   反向学校对学生

class School(Base):
    __tablename__ = "school"
    id = Column(Integer, primary_key=True)
    name = Column(String(32))


from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://root:root@127.0.0.1:3306/sqlalchemy?charset=utf8")

Base.metadata.create_all(engine)  # 创建所有继承Base类对象表